package zack.project.trigger.job;

import zack.project.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import zack.project.domain.activity.service.IRaffleActivitySkuStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**更新数据库表{raffle_activity_sku}的sku商品库存
 * @author A1793
 */
@Slf4j
@Component
public class UpdateActivitySkuStockJob {

    @Resource
    private IRaffleActivitySkuStockService skuStock;

    @Scheduled(cron = "0/5 * * * * ?")
    public void exec() {

        try{
            log.info("定时任务，更新活动sku库存【延迟队列获取，降低对数据库的更新频次，不要产生竞争】");
            ActivitySkuStockKeyVO activitySkuStockKeyVO = skuStock.takeQueueValue();
            if(activitySkuStockKeyVO == null) {
                return;
            }
            log.info("定时任务，更新活动sku库存 sku:{} activityId:{}", activitySkuStockKeyVO.getSku(), activitySkuStockKeyVO.getActivityId());

            skuStock.updateActivitySkuStock(activitySkuStockKeyVO.getSku());
        }catch (Exception e){
            log.error("定时任务，更新活动sku库存失败", e);

        }
    }
}
